#include "subject.h"
#include <iostream>
#include <algorithm>

using std::cout;
using std::cin;
using std::endl;

Subject::Subject()
{}

Subject::~Subject()
{}

void Subject::Attach(Observer* ob)
{
	if (NULL != ob)
	{
		this->ob_list_.push_back(ob);
		cout << "attach a new observer-" << ob->GetName() << endl;
	}
}

void Subject::Detach(Observer* ob)
{
	list<Observer*>::iterator iter;
	iter = find(this->ob_list_.begin(), this->ob_list_.end(), ob);
	if (iter != this->ob_list_.end())
	{
		this->ob_list_.erase(iter);
		cout << "detach an observer-" << ob->GetName() << endl;
	}
}

void Subject::Notify()
{
	list<Observer*>::iterator iter = this->ob_list_.begin();
	for (; iter != this->ob_list_.end(); ++iter)
	{
		(*iter)->Update(this);
	}
}

void Subject::SetState(string state)
{
	this->state_ = state;
}

string Subject::GetState()
{
	return this->state_;
}

ConcreteSubjectA::ConcreteSubjectA()
{}

ConcreteSubjectA::~ConcreteSubjectA()
{}

void ConcreteSubjectA::DoSomething()
{
	this->SetState("上班");
	cout << "打卡上班..." << endl;
}

ConcreteSubjectB::ConcreteSubjectB()
{}

ConcreteSubjectB::~ConcreteSubjectB()
{}

void ConcreteSubjectB::DoSomething()
{
	this->SetState("下班");
	cout << "打卡下班!!!" << endl;
}